# Maintainer: Thomas Bächler <thomas@archlinux.org>
pkgname=ovmf
pkgver=r24601.6861765935
epoch=1
pkgrel=1
arch=('any')
pkgdesc="Tianocore UEFI firmware for qemu."
url="http://sourceforge.net/apps/mediawiki/tianocore/index.php?title=EDK2"
license=('custom')
makedepends=('git' 'python2' 'iasl' 'nasm' 'subversion' 'perl-libwww')
_commit=6861765935d5b69803321ba6e43240845c7ab0e5
_openssl_ver=1.1.0h
source=(edk2::git+https://github.com/tianocore/edk2#commit=$_commit
        https://www.openssl.org/source/openssl-$_openssl_ver.tar.gz{,.asc})
sha256sums=('SKIP'
            '5835626cde9e99656585fc7aaa2302a73a7e1340bf8c14fd635a62c66802a517'
            'SKIP')
validpgpkeys=(8657ABB260F056B1E5190839D9C4D26D0E604491)
options=(!makeflags)
_toolchain_opt=GCC5

pkgver() {
  cd "${srcdir}"/edk2
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}"
  # edk2 uses python everywhere, but expects python2
  mkdir -p bin
  ln -sf /usr/bin/python2 bin/python
  rm -rf $srcdir/edk2/CryptoPkg/Library/OpensslLib/openssl
  ln -sf $srcdir/openssl-$_openssl_ver $srcdir/edk2/CryptoPkg/Library/OpensslLib/openssl
}

build() {
  if [ "$CARCH" != "x86_64" ]; then
    error "This package must be built under the x86_64 architecture."
    false
  fi
  export PATH="${srcdir}/bin:$PATH"
  cd "${srcdir}/"edk2
  make -C BaseTools
  export EDK_TOOLS_PATH="${srcdir}"/edk2/BaseTools
  . edksetup.sh BaseTools

  ./BaseTools/BinWrappers/PosixLike/build -t $_toolchain_opt -a X64 -p OvmfPkg/OvmfPkgX64.dsc -n $(nproc) -b RELEASE -D FD_SIZE_2MB -D NETWORK_IP6_ENABLE -D TPM2_ENABLE -D SECURE_BOOT_ENABLE -D HTTP_BOOT_ENABLE -D TLS_ENABLE

  # build fails for 32bit binary
  # ./BaseTools/BinWrappers/PosixLike/build -t $_toolchain_opt -a IA32 -p OvmfPkg/OvmfPkgIa32.dsc -n $(nproc) -b RELEASE -D FD_SIZE_2MB
}

package() {
  #install -D -m644 "${srcdir}"/edk2/Build/OvmfIa32/RELEASE_${_toolchain_opt}/FV/OVMF_CODE.fd "${pkgdir}"/usr/share/ovmf/ia32/OVMF_CODE.fd
  #install -D -m644 "${srcdir}"/edk2/Build/OvmfIa32/RELEASE_${_toolchain_opt}/FV/OVMF_VARS.fd "${pkgdir}"/usr/share/ovmf/ia32/OVMF_VARS.fd
  install -D -m644 "${srcdir}"/edk2/Build/OvmfX64/RELEASE_${_toolchain_opt}/FV/OVMF_CODE.fd "${pkgdir}"/usr/share/ovmf/x64/OVMF_CODE.fd
  install -D -m644 "${srcdir}"/edk2/Build/OvmfX64/RELEASE_${_toolchain_opt}/FV/OVMF_VARS.fd "${pkgdir}"/usr/share/ovmf/x64/OVMF_VARS.fd
  install -D -m644 "${srcdir}"/edk2/OvmfPkg/License.txt "${pkgdir}"/usr/share/licenses/ovmf/License.txt
}
